<?php
    class Produit {
        //Alias sur table : prod
        public $id_produit;
        public $marque;
        public $libelle;
        public $reference;
        public $description;
        public $description_longue;
        public $stock;
        public $prix_unit;
        public $tva;
        public $promotion;
        public $vente_privee;
        public $date_ajout;
        public $date_modif;
        public $valide;
		
        //Constructeur complet
        //(Marque)marque, (Tva)tva
        public function __construct($id_produit, $marque, $libelle, $reference, $description, $description_longue, $stock, $prix_unit, $tva, 
                                    $promotion, $vente_privee, $date_ajout, $date_modif, $valide) {
            $this->setId($id_produit);
            $this->setMarque($marque);
            $this->setLibelle($libelle);
            $this->setReference($reference);
            $this->setDescription($description);
            $this->setDescriptionLongue($description_longue);
            $this->setStock($stock);
            $this->setPrixUnit($prix_unit);
            $this->setTva($tva);
            $this->setPromotion($promotion);
            $this->setVentePrivee($vente_privee);
            $this->setDateAjout($date_ajout);
            $this->setDateModif($date_modif);
            $this->setValide($valide);
        }
	
        //Retourne le libelle du Produit
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->libelle;
        }
        
        //Ajoute un produit à la BDD
        //Paramètre : aucun
        //Retourne (Produit)
        public function add() {
            $this->setDateAjout(new DateTime());
            $this->setDateModif(new DateTime());
            $sql =  'INSERT INTO produit (id_marque, libelle, reference, description, description_longue, prix_unit, id_tva, promotion, 
                                         vente_privee, date_ajout, date_modif, valide, stock) VALUES 
                    ('.intval($this->marque->getId()).', "'.Db::insertion($this->libelle).'", "'.Db::insertion($this->reference).'", 
                    "'.Db::insertion($this->description).'", "'.Db::insertion($this->description_longue).'", '.$this->prix_unit.', 
                    '.intval($this->tva->getId()).', '.intval($this->promotion).', '.intval($this->vente_privee).', 
                    "'.$this->getDateAjout().'", "'.$this->getDateModif().'", '.intval($this->valide).', '.intval($this->stock).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
		
        //Modifie un produit dans la BDD
        //Paramètre : aucun
        //Retourne (Produit)
        public function update() {
            $sql =  'UPDATE produit
                    SET id_marque = '.intval($this->marque->getId()).', 
                        libelle = "'.Db::insertion($this->libelle).'",
                        reference = "'.Db::insertion($this->reference).'",
                        description = "'.Db::insertion($this->description).'",
                        description_longue = "'.Db::insertion($this->description_longue).'", 
                        prix_unit = '.$this->prix_unit.', 
                        id_tva = '.intval($this->tva->getId()).',
                        promotion = '.intval($this->promotion).', 
                        vente_privee = '.intval($this->vente_privee).', 
                        date_modif = "'.$this->getDateModif().'", 
                        valide = '.intval($this->valide).', 
                        stock = '.intval($this->stock).'
                    WHERE
                        id_produit = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
		
        //Désactive le produit dans la BDD
        //Paramètre : aucun
        //Retourne (Produit)
        public function suppr() {
            $sql =  'UPDATE produit
                    SET valide = '.intval($this->valide).'
                    WHERE
                        id_produit = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
	
        //Retourne les ProduitImage de ce Produit
        //Paramètre : OPTIONNEL (Booléen)cover
        //Retourne collection de ProduitImage
        public function getImages($cover = false) {
            $sql =  'SELECT
                        pi.id_produit_image as piid_produit_image, pi.cover as picover
                    FROM produit_image pi
                    WHERE
                        pi.id_produit = '.intval($this->getId());
            if($cover) {
                $sql .= ' AND pi.cover = 1';
            }
            else {
                $sql .= ' AND pi.cover = 0';
            }
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                if($cover) {
                    return new ProduitImage($p['piid_produit_image'], $this, $p['picover']);
                }
                else {
                    $tabRetour[] = new ProduitImage($p['piid_produit_image'], $this, $p['picover']);
                }
            }
            return $tabRetour;
        }
        
        //Récupère la première Categorie du Produit
        //Paramètre : aucun
        //Retourne (Categorie)
        public function getCategorie() {
            $sql =  'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout, 
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat
                    WHERE
                        cat.id_categorie IN  (SELECT
                                                cp.id_categorie
                                            FROM categorie_produit cp
                                            WHERE
                                                cp.id_produit = '.intval($this->getId()).'
                                            )';
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                return new Categorie($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], 
                                    $p['catdate_modif'], $p['catvalide']);
            }
        }
        
        //Récupère toutes les images pour ce produit
        //Paramètre : aucun
        //Retourne une collection de ProduitImage
        public function getProduitImages() {
            $sql =  'SELECT
                        pi.id_produit_image as piid_produit_image, pi.cover as picover
                    FROM produit_image pi
                    ORDER BY pi.cover DESC';
            $tabRetour = array();
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $tabRetour[] = new ProduitImage($p['piid_produit_image'], $this, $p['picover']);
            }
            return $tabRetour;
        }
        
        //Récupère le prix actuel de cet article
        //Paramètre : OPTIONNEl (Booléen)ttc, (Booléen)promotion
        //Retourne (double)
        public function getPrixActuel($ttc = true, $promotion = true) {
            if($ttc) {
                $temp = ($this->prix_unit*(1 + $this->tva->getTaux()/100));
            }
            else {
                $temp = ($this->prix_unit);
            }
            if($promotion) {
                $temp = $temp * (1 - $this->promotion/100);
            }
            return number_format($temp, 2);
        }
        
        //Récupère tout les produits qui correspondent aux paramètres mis en recherche
        //Paramètre : OPTIONNEL (int)id_produit, (String)reference, (String)libelle
        //Retourne collection de Produit
        public static function filtreProduit($id_produit = NULL, $reference = "", $libelle = "") {
            $sql =  'SELECT
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM produit prod
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        1 = 1';
            if(!empty($id_produit)) {
                $sql .= ' AND prod.id_produit = '.intval($id_produit);
            }
            if(!empty($reference)) {
                $sql .= ' AND prod.reference LIKE "%'.Db::insertion($reference).'%"';
            }
            if(!empty($libelle)) {
                $sql .= ' AND prod.libelle LIKE "%'.Db::insertion($libelle).'%"';
            }
            $sql .= ' ORDER BY prod.id_produit';
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $tabRetour[] = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
            }
            return $tabRetour;
        }
        
        //Rechercher tous les produits actuellement en promotion ( pas uniquement en vente pv )
        //Paramètre : OPTIONNEL (Booléen)vente_privee
        //Retourne les produits en promotion
        public static function getProduitsPromo($vente_privee = false) {
            $sql =  'SELECT
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM produit prod
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        prod.promotion > 0';
            if(!$vente_privee) {
                $sql .= ' AND prod.vente_privee = 0 ';
            }

            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $tabRetour[] = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
            }
            return $tabRetour;
            
        }
        
        //Rechercher tous les produits 
        //Paramètres: Aucun
        //Retourne les produits 
        public static function getProduits() {
            $sql =  'SELECT
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM produit prod
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        prod.promotion <> 5';
 

            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $tabRetour[] = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
            }
            return $tabRetour;
            
        }
     
        //Crée un objet Produit grâce à son ID
        //Paramètre : (int)id
        //Retourne (Produit)
        public static function rechercheProduit($id) {
            $sql =  'SELECT
                            tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                            ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                            ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                            prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                            prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                            prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                            prod.valide as prodvalide, prod.stock as prodstock
                    FROM produit prod
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        prod.id_produit = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                return new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
            }
            return null;
        }
		
        //GETTERS ET SETTERS
        public function getId() {return $this->id_produit;}
        public function getMarque() {return $this->marque;}
        public function getLibelle() {return $this->libelle;}
        public function getReference() {return $this->reference;}
        public function getDescription() {return $this->description;}
        public function getDescriptionLongue() {return $this->description_longue;}
        public function getStock() {return $this->stock;}
        public function getPrixUnit() {return $this->prix_unit;}
        public function getTva() {return $this->tva;}
        public function getPromotion() {return $this->promotion;}
        public function getVentePrivee() {return $this->vente_privee;}
        public function getDateAjout($format = 'Y-m-d H:i:s') {return Tools::getterDate($this->date_ajout, $format);}
        public function getDateModif($format = 'Y-m-d H:i:s') {return Tools::getterDate($this->date_modif, $format);}
        public function getValide() {return $this->valide;}

        public function setId($temp) {$this->id_produit = intval($temp);}
        public function setMarque($temp) {$this->marque = $temp;}
        public function setLibelle($temp) {$this->libelle = $temp;}
        public function setReference($temp) {$this->reference = $temp;}
        public function setDescription($temp) {$this->description = $temp;}
        public function setDescriptionLongue($temp) {$this->description_longue = $temp;}
        public function setStock($temp) {$this->stock = intval($temp);}
        public function setPrixUnit($temp) {
            $temp = str_replace(",", ".", $temp);
            $this->prix_unit = number_format((Double)$temp, 2, '.', '');
        }
        public function setTva($temp) {$this->tva = $temp;}
        public function setPromotion($temp) {$this->promotion = intval($temp);}
        public function setVentePrivee($temp) {$this->vente_privee = intval($temp);}
        public function setDateAjout($temp) {$this->date_ajout = Tools::setterDate($temp);}
        public function setDateModif($temp) {$this->date_modif = Tools::setterDate($temp);}
        public function setValide($temp) {$this->valide = intval($temp);}
    }

?>
